# Maintainer: Justin Kromlinger <hashworks@archlinux.org>

pkgname=vector
pkgver=0.28.2
pkgrel=1
pkgdesc="A high-performance observability data pipeline"
arch=("loong64" "x86_64")
_target="x86_64-unknown-linux-gnu"
url="https://vector.dev"
license=("MPL2")
options=(!lto) # TODO: Build with LTO
backup=(
    "etc/vector/vector.toml"
)
replaces=("vector-bin")
conflicts=("vector-bin" "vector-git")
depends=("gcc-libs" "zlib")
# https://github.com/vectordotdev/vector/blob/master/docs/DEVELOPING.md#bring-your-own-toolbox
makedepends=(
    "rustup"
    "protobuf"
    "python"
    "perl"
    "cmake"
)
checkdepends=(
    "cargo-nextest"
)
source=(
    "${pkgname}-${pkgver}.tar.gz::https://github.com/vectordotdev/vector/archive/refs/tags/v${pkgver}.tar.gz"
    "${pkgname}.sysusers"
    "${pkgname}.tmpfiles"
)
sha512sums=('5326f333a560bcb06a4eb048b6d950dfaa5e512abae0d74d439ab7f2cbdb2e0f70b2c712630c8ff2876e891f1994f906c739fd7e457dbd6a160ed617ec37ebbf'
            '4f7a5d63f4e89018d1f0e9aa0fba2bb5c448d7031a7ff40c82878f574243e075c2fca020e78c4c71b1caa1a5bd1f06b496a5d3ab432f01d145233fd6c9ec4b87'
            'c192492df09d131f9174d2acbb7f265c280eb6d678589b8c93bacc47ae55c51573a5477d715897f8580ced420730992fb68bee78b374f1cc042888ea6b5512fd')

prepare() {
    cd "${pkgname}-${pkgver}"

    cargo fetch \
        --locked
}

build() {
    cd "${pkgname}-${pkgver}"
    cargo build \
        --frozen \
        --release \
        --locked \
        --target "${_target}"

    # Since cargo nextest drops our release build, we need to back it up
    cp -r target/${_target}/release releasebuild
}

check() {
    cd "${pkgname}-${pkgver}"

    # Unit-Tests only, integration tests require services
    cargo nextest run \
        --workspace \
        --fail-fast \
        --test-threads num-cpus \
        --frozen \
        --release \
        --locked \
        --offline \
        --no-default-features \
        --target "${_target}"
}

package() {
    install -Dm644 "${pkgname}.sysusers" "${pkgdir}/usr/lib/sysusers.d/${pkgname}.conf"
    install -Dm644 "${pkgname}.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/${pkgname}.conf"

    cd "${pkgname}-${pkgver}"

    install -Dm644 "LICENSE" "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
    install -Dm644 "NOTICE" "${pkgdir}/usr/share/licenses/${pkgname}/NOTICE"

    install -Dm644 "README.md" "${pkgdir}/usr/share/doc/${pkgname}/README.md"
    install -Dm644 "README.md" "${pkgdir}/usr/share/doc/${pkgname}/PRIVACY.md"
    install -Dm644 "README.md" "${pkgdir}/usr/share/doc/${pkgname}/RELEASES.md"
    install -Dm644 "README.md" "${pkgdir}/usr/share/doc/${pkgname}/VERSIONING.md"
    install -Dm644 "README.md" "${pkgdir}/usr/share/doc/${pkgname}/RELEASES.md"
    install -Dm644 "README.md" "${pkgdir}/usr/share/doc/${pkgname}/CHANGELOG.md"

    install -Dm755 "releasebuild/vector" "${pkgdir}/usr/bin/vector"

    install -Dm644 "config/vector.toml" "${pkgdir}/etc/vector/vector.toml"
    cp -r config/examples "${pkgdir}/usr/share/doc/${pkgname}/examples"

    install -Dm644 "distribution/systemd/vector.service" "${pkgdir}/usr/lib/systemd/system/vector.service"
    install -Dm644 "distribution/systemd/hardened-vector.service" "${pkgdir}/usr/lib/systemd/system/hardened-vector.service"
    install -Dm644 "distribution/systemd/vector.default" "${pkgdir}/etc/default/vector"
}
